package com.liuceng.zen.service;

import com.alibaba.druid.sql.parser.Token;
import com.alibaba.fastjson.JSON;
import com.github.kevinsawicki.http.HttpRequest;
import com.liuceng.zen.lib.gdt.vo.GdtResult;
import com.liuceng.zen.mapper.LandingPageMapper;
import com.liuceng.zen.pojo.domain.LandingPage;
import com.liuceng.zen.pojo.domain.Member;
import com.liuceng.zen.pojo.form.TokenForm;
import com.liuceng.zen.web.controller.CampaignController;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by yanwen on 16-9-14.
 * LandingPageService :
 */
@Service
@Data
public class LandingPageService extends BaseService<LandingPage>{
    private final static Logger LOG = LoggerFactory.getLogger(LandingPageService.class);
    @Value("${diy_Form_domain}")
    public String diyFormDomain;
    @Value("${main_domain}")
    public String mainDomain;
    @Autowired
    public LandingPageMapper landingPageMapper;
    /**
     * 删除
     * @return
     */
    public Integer delete(Integer landingPageId){
        Map<String,Object> map=new HashMap<>();
        map.put("landingPageId",landingPageId);
        map.put("deletedAt",new Date());
        landingPageMapper.delete(map);
        return landingPageId;
    }

    /**
     * 获取token
     * @param landingPage
     * @return
     */
    public String getToken(LandingPage landingPage){
        Map<String,Object> map=new HashMap<>();
        map.put("member_id",landingPage.getMemberId());
        map.put("show_type",landingPage.getShowType());
        map.put("name",landingPage.getName());
        map.put("id",landingPage.getId());
        HttpRequest req =HttpRequest.get("http://"+diyFormDomain+"/zen/front/token.json", false,map2arr(map) );
        if (200 != req.code()) {
           LOG.error("生成token失败 read error:" + req.code() + "\r\n body:" + req.body());
            return null;
        }
        TokenForm tokenForm=JSON.parseObject(req.body(), TokenForm.class);
        return  tokenForm.getToken();
    }

    /**
     * 获取正在使用中的落地页数量
     * @param landingPageId
     * @return
     */
    public Integer getUseCount(Integer landingPageId,Integer memberId,Integer showType){
        Map<String,Object> map=new HashMap<>();
        map.put("memberId",memberId);
        map.put("landingPageId",landingPageId);
        map.put("showType",showType);
        List<LandingPage> landingPages=landingPageMapper.findByParams(map);
        return landingPages==null?0:landingPages.size();
    }

    /**
     * 创建广告创意
     * @param landingPage
     * @param member
     * @return
     */
    @Transactional
    public Integer createLandingPage(LandingPage landingPage, Member member){
        landingPage.setMemberId(member.getId());
        landingPage.setDepartmentId(member.getAdminDepartmentId());
        this.insert(landingPage);
        String token=this.getToken(landingPage);
        landingPage.setToken(token);
        this.update(landingPage);
        return landingPage.getId();
    }

    public static void main(String[] args) {
        Map<String,Object> map=new HashMap<>();
        map.put("member_id",3);
        map.put("show_type",2);
        map.put("name","空白落地页");
        HttpRequest req =HttpRequest.get("http://exform.hlt.com:4000/zen/front/token.json",false,map2arr(map));
        System.out.println(req.body());
    }

    /**
     * 将map转换成数组
     * @param params
     * @return
     */
    public static Object[] map2arr(Map<String, Object> params) {
        if (params == null || params.isEmpty()) {
            return null;
        }
        Object[] keys = params.keySet().toArray();
        Object[] vals = params.values().toArray();
        List<Object> objList = new ArrayList<>();
        for (int i = 0; i < keys.length; i++) {
            Object key = keys[i];
            Object val = vals[i];
            if (val != null) {
                objList.add(key);
                objList.add(val);
            }
        }
        return objList.toArray();
    }



}
